<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>聊天对话框</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f7f7f7;
            margin: 0;
            padding: 20px;
        }

        #chat-container {
            width: 400px;
            height: 500px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            overflow-y: scroll;
            margin: 0 auto;
            padding: 20px;
            box-sizing: border-box;
        }

        .message {
            margin-bottom: 10px;
            padding: 10px 20px;
            border-radius: 5px;
        }

        .user-message {
            background-color: #007bff;
            color: white;
            align-self: flex-end;
        }

        .bot-message {
            background-color: #28a745;
            color: white;
            align-self: flex-start;
        }

        #input-container {
            display: flex;
            margin-top: 20px;
        }

        #chat-input {
            flex-grow: 1;
            margin-right: 10px;
            padding: 10px;
            border-radius: 5px;
            border: none;
            font-size: 16px;
            resize: vertical; /* 允许垂直调整文本框大小 */
        }

        #send-button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <div id="chat-container" >
       <!--  <div class="message user-message">Hello!</div> --> 
        <div class="message bot-message">我是讯飞星火认知大模型，我可以通过自然语言交互的方式，为您提供包括语言理解、问答、推理等各类认知智能服务。</div>
    </div>

    <div id="input-container" style="width: 50%; margin:0 auto;">
        <textarea id="chat-input" rows="3"></textarea>
        <div>
        <button id="send-button" style="height: 35px;">发送</button>
        </div>
        <div>
        <button id="btn_control" style="margin-top: 50px;
    margin-left: -70px;
    height: 35px;
    background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;">录音</button>
        </div>
    </div>
    
    <div id="result"></div>
	
	<script src="js/axios.js" type="text/javascript"></script>
	<script src="js/qs.js" type="text/javascript"></script>
	
	<!-- 录音相关  -->
	<script src="iat-js-demo/example/crypto-js.js"></script>
    <script src="iat-js-demo/dist/index.umd.js"></script>
    <script src="iat-js-demo/example/iat/index.js"></script>
	
	<script>
      var APPID = "aca327b7";
      var API_SECRET = "M2RjNTk2MTU2MTAyYzIzYTZlZWZhZWVk";
      var API_KEY = "b6fe6b8c155530582f1eaf7662bf0eff";
    </script>
	<!-- 结束 -->
	
    <script>
        const chatContainer = document.getElementById('chat-container');
        const chatInput = document.getElementById('chat-input');
        const sendButton = document.getElementById('send-button');
        let oldStr="";
        
        function addMessage(message, isUser) {
            const messageElement = document.createElement('div');
            messageElement.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
            messageElement.textContent = message;
            chatContainer.appendChild(messageElement);
            chatContainer.scrollTop = chatContainer.scrollHeight;
        }
        
        function sendMessage() {
            const userMessage = chatInput.value.trim();
            let botMessage = '';
           
            if (userMessage !== '') {
            	 addMessage(userMessage, true);
                 chatInput.value = '';
            	// 发送请求
            	axios.defaults.timeout = 30000;
            	
            	axios.post("spark/spark", qs.stringify({problem: userMessage})).then(rt => {
        			
        			botMessage = rt.data.answer;
        			
        			// 使用split()函数将字符串分割成数组
        			//let stringArray = botMessage.split(" ");
        			//console.info(stringArray);
        			// 使用数组中的最后一个元素作为最后的子字符串
        			//let lastWord = stringArray[stringArray.length - 2];
        			
        			let newString = botMessage.replace(oldStr, "");
        			oldStr += newString;
        			console.info("old" + oldStr);
        			console.info("new" + newString);
        			
        			botMessage = newString;
        			
                    // 在这里执行发送消息给机器人的操作，并接收响应
                    addMessage(botMessage, false);
        		})
            }
        }
        
        sendButton.addEventListener('click', sendMessage);
        chatInput.addEventListener('keydown', function (event) {
            if (event.key === 'Enter' && !event.shiftKey) {
                event.preventDefault(); // 阻止 Enter 键触发换行
                sendMessage();
            }
        });
    </script>
</body>
</html>